Purpose You want to capture a screenshot of a control that you have created
with the "Control" statement.
Solution
Use the VB.NET function "capture" given below.
The screenshot is saved as an image file and you can use it in the same
InputScript, for example to build up an MS Word file for a PDF file.
Remarks
As a special case you can capture the content of the web browser
control In this case, first wait until the web page is fully loaded (VB
function see below)
The "capture" function also works for controls started with the
-invisible option, allowing you
to generate images from HTML pages or Active controls without
displaying the controls
Example
We generate two .png image files
via a hidden control:
The first image file captures a webcam via a URL
The second image file captures a chart generated via JavaScript;
we use the example in Using JavaScript frameworks
InputScript
Remarks: For our control
"mycontrol", the variable V[mycontrol] is passed as object pointer from
GuiXT to VB.NET. The variable V[mycontrol.hwnd] is the Window handle of the
control that we need for generating the screenshot.
GuiXT
// open an invisible HTML control with a webcam URL
Control (1,1) (30,200) _
name="mycontrol" _
progID="https://www.foto-webcam.eu/webcam/funtenseetauern/" _
-floating -invisible
// wait until HTML page is complete
CallVB utilities.control.wbComplete "&V[mycontrol]"
// capture a screenshot
CallVB utilities.control.capture "&V[mycontrol.hwnd]" "C:\temp\screenshot1.png"
// close the control
CloseControl name="mycontrol"
// open a blank HTML control
Control (2.7,6.3) (18,85.4) _
name="mycontrol" _
progID="about:blank" _
-floating -invisible
// generate a chart via JavaScript
CallJS display_chart "&V[mycontrol]"
// capture a screenshot
CallVB utilities.control.capture "&V[mycontrol.hwnd]" "C:\temp\screenshot2.png"
// close the control
CloseControl name="mycontrol"
// we now can use the two image files for further actions ...
Return
Files generated
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports SHDocVw
Public Class control
Public Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
Public Enum WINDOWSTAT As Integer
SW_HIDE = 0
SW_NORMAL = 1
End Enum
' Windows functions
Private Declare Function GetWindowRect Lib "user32.dll" _
(ByVal hWnd As IntPtr,_
ByRef lpRect As RECT) As Boolean
Private Declare Function PrintWindow Lib "user32.dll" _
(ByVal hwnd As IntPtr, ByVal hDC As IntPtr, _
ByVal nFlags As UInteger) As Boolean
Private Declare Function ShowWindow Lib "user32.dll" _
(ByVal hWnd As IntPtr, _
ByVal nCmdShow As Integer) As Boolean
Private Declare Function InvalidateRect Lib "user32.dll" _
(ByVal hWnd As IntPtr,
ByVal lpRect As IntPtr, _
ByVal bErase As Boolean) As Boolean
Private Declare Function MoveWindow Lib "user32.dll" _
(ByVal hWnd As IntPtr, _
ByVal X As Int32, _
ByVal Y As Int32, _
ByVal Width As Int32, _
ByVal Height As Int32,
ByVal Repaint As Boolean) As Boolean
Private Declare Function IsWindowVisible Lib "user32.dll" _
(ByVal hwnd As Long) As Boolean
' wait until web browser page is fully loaded
Public Sub wbComplete(ie As SHDocVw.WebBrowser)
Try
While ie.Busy OrElse _
ie.ReadyState <> tagREADYSTATE.READYSTATE_COMPLETE
System.Threading.Thread.Sleep(10)
Application.DoEvents()
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' capture window screenshot
Public Sub capture(charhwnd As String, filename As String)
Try
Dim hwnd As Integer = CInt(charhwnd)
' get window size
Dim r As New RECT
GetWindowRect(hwnd, r)
Dim image As Image = _
New Bitmap(r.right - r.left, r.bottom - r.top)
Dim screenshot As Graphics = Graphics.FromImage(image)
Dim isHidden As Boolean = Not IsWindowVisible(hwnd)
If isHidden Then
' move window out of screen to avoid flickering
MoveWindow(hwnd, r.left, r.top + 8000, _
r.right - r.left, r.bottom - r.top, True)
' make window visible
ShowWindow(hwnd, WINDOWSTAT.SW_NORMAL)
End If
InvalidateRect(hwnd, IntPtr.Zero, True)
Application.DoEvents()
' print client area of window to bitmap
Dim PW_CLIENTONLY As Integer = 1
PrintWindow(hwnd, screenshot.GetHdc, PW_CLIENTONLY)
screenshot.ReleaseHdc()
If isHidden Then
' hide window
ShowWindow(hwnd, WINDOWSTAT.SW_HIDE)
' restore window position
MoveWindow(hwnd, r.left, r.top, _
r.right - r.left, r.bottom - r.top, True)
Application.DoEvents()
End If
image.Save(filename, _
System.Drawing.Imaging.ImageFormat.Png)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using SHDocVw;
public class control
{
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public enum WINDOWSTAT : int
{
SW_HIDE = 0,
SW_NORMAL = 1
}
// Windows functions
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll")]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC,
uint nFlags);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool InvalidateRect(IntPtr hWnd,
IntPtr lpRect, bool bErase);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y,
int Width, int Height, bool Repaint);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
// wait until web browser page is fully loaded
public void wbComplete(WebBrowser ie)
{
try
{
while (ie.Busy ||
ie.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE)
{
Thread.Sleep(10);
Application.DoEvents();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// capture window screenshot
public void capture(string charhwnd, string filename)
{
try
{
IntPtr hwnd = new IntPtr(Convert.ToInt32(charhwnd));
// get window size
RECT r = new RECT();
GetWindowRect(hwnd, ref r);
Bitmap image = new Bitmap(r.right - r.left, r.bottom - r.top);
Graphics screenshot = Graphics.FromImage(image);
bool isHidden = !IsWindowVisible(hwnd);
if (isHidden)
{
// move window out of screen to avoid flickering
MoveWindow(hwnd, r.left, r.top + 8000,
r.right - r.left, r.bottom - r.top, true);
// make window visible
ShowWindow(hwnd, (int)WINDOWSTAT.SW_NORMAL);
}
InvalidateRect(hwnd, IntPtr.Zero, true);
Application.DoEvents();
// print client area of window to bitmap
const uint PW_CLIENTONLY = 1;
IntPtr hdc = screenshot.GetHdc();
PrintWindow(hwnd, hdc, PW_CLIENTONLY);
screenshot.ReleaseHdc(hdc);
if (isHidden)
{
// hide window
ShowWindow(hwnd, (int)WINDOWSTAT.SW_HIDE);
// restore window position
MoveWindow(hwnd, r.left, r.top,
r.right - r.left, r.bottom - r.top, true);
Application.DoEvents();
}
image.Save(filename, ImageFormat.Png);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}